﻿using System;
using GeoAPI.Geometries;

namespace NetTopologySuite.Algorithm
{
	public static class NonRobustCGAlgorithms
	{
		public static bool IsPointInRing(Coordinate p, Coordinate[] ring)
		{
			int num = 0;
			int num2 = ring.Length;
			for (int i = 1; i < num2; i++)
			{
				int num3 = i - 1;
				Coordinate coordinate = ring[i];
				Coordinate coordinate2 = ring[num3];
				double num4 = coordinate.X - p.X;
				double num5 = coordinate.Y - p.Y;
				double num6 = coordinate2.X - p.X;
				double num7 = coordinate2.Y - p.Y;
				if ((num5 > 0.0 && num7 <= 0.0) || (num7 > 0.0 && num5 <= 0.0))
				{
					double num8 = (num4 * num7 - num6 * num5) / (num7 - num5);
					if (0.0 < num8)
					{
						num++;
					}
				}
			}
			return num % 2 == 1;
		}

		public static bool IsCCW(Coordinate[] ring)
		{
			int num = ring.Length - 1;
			if (num < 4)
			{
				return false;
			}
			Coordinate coordinate = ring[0];
			int num2 = 0;
			for (int i = 1; i <= num; i++)
			{
				Coordinate coordinate2 = ring[i];
				if (coordinate2.Y > coordinate.Y)
				{
					coordinate = coordinate2;
					num2 = i;
				}
			}
			int num3 = num2;
			do
			{
				num3 = (num3 - 1) % num;
			}
			while (ring[num3].Equals(coordinate) && num3 != num2);
			int num4 = num2;
			do
			{
				num4 = (num4 + 1) % num;
			}
			while (ring[num4].Equals(coordinate) && num4 != num2);
			Coordinate coordinate3 = ring[num3];
			Coordinate coordinate4 = ring[num4];
			if (coordinate3.Equals(coordinate) || coordinate4.Equals(coordinate) || coordinate3.Equals(coordinate4))
			{
				throw new ArgumentException("degenerate ring (does not contain 3 different points)");
			}
			double num5 = coordinate3.X - coordinate.X;
			double num6 = coordinate3.Y - coordinate.Y;
			double num7 = coordinate4.X - coordinate.X;
			double num8 = coordinate4.Y - coordinate.Y;
			double num9 = num7 * num6 - num8 * num5;
			if (num9 == 0.0)
			{
				return coordinate3.X > coordinate4.X;
			}
			return num9 > 0.0;
		}

		public static int ComputeOrientation(Coordinate p1, Coordinate p2, Coordinate q)
		{
			return NonRobustCGAlgorithms.OrientationIndex(p1, p2, q);
		}

		public static int OrientationIndex(Coordinate p1, Coordinate p2, Coordinate q)
		{
			double num = p2.X - p1.X;
			double num2 = p2.Y - p1.Y;
			double num3 = q.X - p2.X;
			double num4 = q.Y - p2.Y;
			double num5 = num * num4 - num3 * num2;
			if (num5 > 0.0)
			{
				return 1;
			}
			if (num5 < 0.0)
			{
				return -1;
			}
			return 0;
		}

		public static double DistanceLineLine(Coordinate A, Coordinate B, Coordinate C, Coordinate D)
		{
			if (A.Equals(B))
			{
				return CGAlgorithms.DistancePointLine(A, C, D);
			}
			if (C.Equals(D))
			{
				return CGAlgorithms.DistancePointLine(D, A, B);
			}
			double num = (A.Y - C.Y) * (D.X - C.X) - (A.X - C.X) * (D.Y - C.Y);
			double num2 = (B.X - A.X) * (D.Y - C.Y) - (B.Y - A.Y) * (D.X - C.X);
			double num3 = (A.Y - C.Y) * (B.X - A.X) - (A.X - C.X) * (B.Y - A.Y);
			double num4 = (B.X - A.X) * (D.Y - C.Y) - (B.Y - A.Y) * (D.X - C.X);
			if (num2 == 0.0 || num4 == 0.0)
			{
				return System.Math.Min(CGAlgorithms.DistancePointLine(A, C, D), System.Math.Min(CGAlgorithms.DistancePointLine(B, C, D), System.Math.Min(CGAlgorithms.DistancePointLine(C, A, B), CGAlgorithms.DistancePointLine(D, A, B))));
			}
			double num5 = num3 / num4;
			double num6 = num / num2;
			if (num6 < 0.0 || num6 > 1.0 || num5 < 0.0 || num5 > 1.0)
			{
				return System.Math.Min(CGAlgorithms.DistancePointLine(A, C, D), System.Math.Min(CGAlgorithms.DistancePointLine(B, C, D), System.Math.Min(CGAlgorithms.DistancePointLine(C, A, B), CGAlgorithms.DistancePointLine(D, A, B))));
			}
			return 0.0;
		}
	}
}
